package com.example.medicalzb.controller;

import com.example.medicalzb.param.DoctorParam;
import com.example.medicalzb.service.DoctorService;
import com.example.medicalzb.util.Msg;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

@RestController
@RequestMapping("/api/doctors")
@CrossOrigin
@Api(tags = "医师控制器类")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    //获取医师列表
    @RolesAllowed({"1","2"})
    @GetMapping(value = "")
    public Msg getDoctorWithPage(DoctorParam doctorParam){
        return doctorService.getDoctorWithPage(doctorParam);
    }

    //查询当前医院所有医生可看的病种
    @RolesAllowed({"1","2"})
    @GetMapping("/info")
    public Msg getLevelAndType(){
        return doctorService.getLevelAndType();
    }

    //新增医师
    @RolesAllowed({"1","2"})
    @PostMapping(value = "")
    public Msg saveDoctor(@RequestBody @Validated DoctorParam doctorParam){
        return doctorService.saveDoctor(doctorParam);
    }

    //修改医师信息
    @RolesAllowed({"1"})
    @PutMapping("/{id}")
    public Msg updateDoctor(@PathVariable("id") Long id, @RequestBody DoctorParam doctorParam){
        return doctorService.updateDoctor(id, doctorParam);
    }

    //删除医师
    @RolesAllowed({"1"})
    @DeleteMapping("{id}")
    public Msg deleteDoctor(@PathVariable("id") Long id){
        return doctorService.deleteDoctor(id);
    }

    //重置医师密码
    @RolesAllowed({"1"})
    @PutMapping("/reset/{id}/{pwd}/{repwd}")
    public Msg resetPwd(@PathVariable("id") Long id, @PathVariable("pwd") String pwd, @PathVariable("repwd") String repwd){
        if (pwd.equals(repwd)){
            return doctorService.resetPwd(id,pwd);
        }
        return Msg.fail().mess("修改密码失败");
    }
}
